package io.sage.modules.flow.service.impl;

import com.alibaba.fastjson.JSON;
import io.sage.modules.flow.service.ReadsenseApiService;
import org.apache.http.Header;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Service("readsenseApiService")
public class ReadsenseApiServiceImpl implements ReadsenseApiService {
    @Value("${readsenseapi.url}")
    private String url;
    @Value("${readsenseapi.token}")
    private String token;

    /**
     * 带有json字符串的post请求
     * @param uri
     * @param map
     * @return
     */
    @Override
    public String posts(String uri, Map<String, Object> map) {
        //创建HTTPpost
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost(url + uri);
        StringEntity entity = new StringEntity(JSON.toJSONString(map), "utf-8");
        httpPost.setEntity(entity);
        //readsenseApi下登录时需要的token值
        httpPost.addHeader("Authorization", token);
        httpPost.setHeader("Content-Type", "application/json;charset=utf8");
        System.out.println("httpPost==>"+httpPost);
        CloseableHttpResponse execute = null;
        try {
            //获取并封装到HTTPentity
            execute = httpClient.execute(httpPost);
            HttpEntity entity1 = execute.getEntity();
            //输出值
            return EntityUtils.toString(entity1);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (execute != null) {
                    execute.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return "失败";
    }

    /**
     * 带有参数的get请求,展示用
     * @param uri
     * @return
     */
    @Override
    public Map<String, String> gets(String uri) {
        //创建并使用HTTPget请求方式
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet httpGet = new HttpGet(url + uri);
        //readsenseApi下登录时需要的token值
        httpGet.addHeader("Access-Control-Expose-Headers", "X-Current-Page,X-Per-Page,X-Total-Count");
        httpGet.addHeader("Authorization", token);
        //设置头
        httpGet.setHeader("Content-Type", "application/json;charset=utf8");
        CloseableHttpResponse execute = null;
        try {
            //获取内容并封装到HTTPentity
            execute = httpClient.execute(httpGet);
            Header[] headers = execute.getAllHeaders();
            String total = "";
            for (int i = 0; i < headers.length; i++) {
                if ("X-Total-Count".equals(headers[i].getName())) {
                    total = headers[i].getValue();

                }
            }
            Map<String, String> map = new HashMap<>();
            map.put("total", total);
            HttpEntity entity = execute.getEntity();
            map.put("entity", EntityUtils.toString(entity));
            //输出值
            return map;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (execute != null) {
                    execute.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 带有参数的普通get请求
     * @param uri
     * @return
     */
    @Override
    public String getsagin(String uri) {
        //创建并使用HTTPget请求方式
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpGet httpGet = new HttpGet(url + uri);
        //readsenseApi下登录时需要的token值
        httpGet.addHeader("Authorization", token);
        //设置头
        httpGet.setHeader("Content-Type", "application/json;charset=utf8");
        CloseableHttpResponse execute = null;
        try {
            //获取内容并封装到HTTPentity
            execute = httpClient.execute(httpGet);
            HttpEntity entity = execute.getEntity();
            //输出值
            return EntityUtils.toString(entity);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (execute != null) {
                    execute.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 删除用
     * @param uri
     * @return
     */
    @Override
    public String deletes(String uri) {
        //创建并使用HTTPget请求方式
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpDelete httpDelete = new HttpDelete(url + uri);
        //readsenseApi下登录时需要的token值
        httpDelete.addHeader("Authorization", token);
        //设置头
        httpDelete.setHeader("Content-Type", "application/json;charset=utf8");
        System.out.println(httpDelete+"hrasdscdsc");
        CloseableHttpResponse execute = null;
        try {
            //获取内容并封装到HTTPentity
            execute = httpClient.execute(httpDelete);
            HttpEntity entity = execute.getEntity();
            //输出值
            return EntityUtils.toString(entity);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (execute != null) {
                    execute.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
    /**
     * 顾客增加及修改和普通传参用的post方法
     */
    @Override
    public String postsagin(String uri) {
        //创建并使用HTTPget请求方式
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();
        HttpPost httpPost = new HttpPost(url + uri);
        System.out.println("agin==>"+httpPost);
        //readsenseApi下登录时需要的token值
        httpPost.addHeader("Authorization", token);
        //设置头
        httpPost.setHeader("Content-Type", "application/json;charset=utf8");
        CloseableHttpResponse execute = null;
        try {
            //获取内容并封装到HTTPentity
            execute = httpClient.execute(httpPost);
            HttpEntity entity = execute.getEntity();
            //输出值
            return EntityUtils.toString(entity);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // 释放资源
                if (httpClient != null) {
                    httpClient.close();
                }
                if (execute != null) {
                    execute.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}


